﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Model;

using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Configs;
using SIG.Model.Store;
using SIG.Resources;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class GoodsPhotoService: EntityService<GoodsPhoto>, IGoodsPhotoService
    {
       
        private readonly IGoodsPhotoRepository _productPhotoRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public GoodsPhotoService( IGoodsPhotoRepository productPhotoRepository, ILoggingService loggingService, 
            ICacheService cacheService)
            : base(productPhotoRepository, cacheService, loggingService)
        {
           
            _productPhotoRepository = productPhotoRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        
        public IList<GoodsPhoto> GetPhotos(int goodsId)
        {        
            var key = $"{EntityNames.GoodsPhoto}s_ByProduct_{goodsId}";

            if (!SettingsManager.Store.EnableCaching)
            {
                return _productPhotoRepository.GetFilteredElements((p => p.GoodsId == goodsId)).ToList();
            }

            if (_cacheService.IsSet(key))
            {
                return (List<GoodsPhoto>)_cacheService.Get(key);

            }

            List<GoodsPhoto> photos = _productPhotoRepository.GetFilteredElements((p => p.GoodsId == goodsId)).ToList();
                _cacheService.Set(key, photos, SettingsManager.Store.CacheDuration);
         

            return photos;
        }
        

       
    }
}
